package com.example.helloworld;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * TODO Put here a description of what this class does.
 *
 * @author jrodge01.
 *         Created Apr 15, 2013.
 */
public class Bets {
	// Constants used to identify the proper URLs for grabbing data
	private static final String url_root = "http://rsl.lib.fit.edu/android_connect/";
	private static final String url_getAllBets = url_root + "bet_read_all.php";
	private static final String url_getOneBet = url_root + "bet_read_one.php";
	private static final String url_getBetRange = url_root + "bet_read_range.php";
	private static final String url_createBet = url_root + "bet_create.php";
	private static final String url_updateBet = url_root + "bet_update.php";
	// Limit the bets to 10 for user experience and performance reasons
	private static final int MAX_BETS = 10;
	
	// Necessary variables for getting and parsing data
	private static JSONArray bets = null;
	private static JSONParser jParser = new JSONParser();
	private static ArrayList<HashMap<String, String>> betsList;
	
	// JSON node names
	// These nodes identify different fields and objects in the JSON data
	private static final String TAG_SUCCESS = "success";
	private static final String TAG_BETS = "bets";
	private static final String TAG_BETID = "betID";
	private static final String TAG_BETTORID = "bettorID";
	private static final String TAG_ACCEPTORID = "acceptorID";
	private static final String TAG_VALUE = "value";
	
	/**
	 * This constructor is here as a default
	 *
	 */
	public Bets() {
		
	}
	
	/**
	 * betsList accessor method
	 *
	 * @return a list of all bets.
	 */
	public static ArrayList<HashMap<String, String>> getBets() {
		return betsList;
	}
	
	/**
	 * Sets 'betsList' to be a list of all bets.
	 * If 'betsList' is still null, there are no bets.
	 */
	public static void getAllbets() {
		// Build parameters.
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("MAX_BETS", Integer.toString(MAX_BETS)));
		// getting JSON string from URL
		JSONObject json = jParser.makeHttpRequest(url_getAllBets, "GET", params);
		try {
			// Check the SUCCESS TAG
			int success = json.getInt(TAG_SUCCESS);
			
			if (success == 1) {
				// bets found
				// Get array of bets
				bets = json.getJSONArray(TAG_BETS);
				
				// loop through all bets
				for(int i = 0; i < bets.length(); i++) {
					JSONObject c = bets.getJSONObject(i);
					
					// Store each JSON item into a var.
					String betID = c.getString(TAG_BETID);
					String bettorID = c.getString(TAG_BETTORID);
					String acceptorID = c.getString(TAG_ACCEPTORID);
					String value = c.getString(TAG_VALUE);
					
					// create a new HashMap
					HashMap<String, String> map = new HashMap<String, String>();
					
					// add each child to the hashmap
					map.put(TAG_BETID, betID);
					map.put(TAG_BETTORID, bettorID);
					map.put(TAG_ACCEPTORID, acceptorID);
					map.put(TAG_VALUE, value);
					
					// add HashList to ArrayList
					betsList.add(map);
				}
			} else {
				// no bets found
				// bets will be null
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Populates bets with all bets within the range.
	 *
	 * @param minimum - minimum value of bet
	 * @param maximum - maximum value of bet
	 */
	public static void getBetRange(int minimum, int maximum) {
		// Build parameters.
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("minBet", Integer.toString(minimum)));
		params.add(new BasicNameValuePair("maxBet", Integer.toString(maximum)));
		params.add(new BasicNameValuePair("MAX_BETS", Integer.toString(MAX_BETS)));
		// getting JSON string from URL
		JSONObject json = jParser.makeHttpRequest(url_getBetRange, "GET", params);
		try {
			// Check the SUCCESS TAG
			int success = json.getInt(TAG_SUCCESS);
			
			if (success == 1) {
				// bets found
				// Get array of bets
				bets = json.getJSONArray(TAG_BETS);
				
				
				
				// loop through all bets
				for(int i = 0; i < bets.length(); i++) {
					JSONObject c = bets.getJSONObject(i);
					
					// Store each JSON item into a var.
					String betID = c.getString(TAG_BETID);
					String bettorID = c.getString(TAG_BETTORID);
					String acceptorID = c.getString(TAG_ACCEPTORID);
					String value = c.getString(TAG_VALUE);
					
					// create a new HashMap
					HashMap<String, String> map = new HashMap<String, String>();
					
					// add each child to the hashmap
					map.put(TAG_BETID, betID);
					map.put(TAG_BETTORID, bettorID);
					map.put(TAG_ACCEPTORID, acceptorID);
					map.put(TAG_VALUE, value);
					
					// add HashList to ArrayList
					betsList.add(map);
				}
			} else {
				// no bets found
				// bets will be null
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Accepts the specified bet.
	 *
	 * @param betID - unique ID of the bet.
	 * @param userID - unique ID of the acceptor.
	 */
	public static void acceptBet(int betID, int userID) {
		// Build parameters.
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("betID", Integer.toString(betID)));
		params.add(new BasicNameValuePair("userID", Integer.toString(userID)));
		// getting JSON string from URL
		JSONObject json = jParser.makeHttpRequest(url_updateBet, "POST", params);
		try {
			// Check the SUCCESS TAG
			int success = json.getInt(TAG_SUCCESS);
			
			if (success == 1) {
				// Bet has been accepted.
				
			} else {
				// Bet has not been accepted.
				// TODO Make a new bet of the proposed value.
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Create a new bet.
	 *
	 * @param userID - unique ID of the user.
	 * @param value - value of the proposed bed.
	 */
	public static void createBet(int userID, int value) {
		// Build parameters.
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("value", Integer.toString(value)));
		params.add(new BasicNameValuePair("userID", Integer.toString(userID)));
		// getting JSON string from URL
		JSONObject json = jParser.makeHttpRequest(url_createBet, "POST", params);
		try {
			// Check the SUCCESS TAG
			int success = json.getInt(TAG_SUCCESS);
			
			if (success == 1) {
				// Bet has been accepted.
				
			} else {
				// Bet has not been accepted.
				// Make a new bet of the proposed value.
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
